#include "CharGenerator.h"
#include <stdio.h>
#include <stdlib.h>

CCharGenerator::CCharGenerator(CTimer* aTimer, unsigned int aResolution, ICharGenObserver* aObserver)
{
	iTimer = aTimer;
	iResolution = aResolution;
	iObserver = aObserver;
}

CCharGenerator::~CCharGenerator(void)
{
}

void CCharGenerator::Finished()
{
	if(iIndex < iStrValue.size())
	{
		unsigned int revIndex = iStrValue.size() - 1 - iIndex;
		iObserver->CharEvent(iStrValue[revIndex], revIndex);
		iTimer->Wait(iInterval, this);
	}
	else
	{
		iTimer->RemoveObserver();
		iObserver->CharGenFinished();
	}
			
	iIndex++;
}

void CCharGenerator::Start(unsigned int aInterval, unsigned int aValue)
{
	char buffer [20];
	itoa(aValue,buffer,10);
	iStrValue = buffer;
	iIndex = 0;
	iInterval = aInterval;

	iTimer->Wait(500, this);
}	
